const args = require('minimist')(process.argv.slice(2)) // minimist用来解析命令行参数的
const {resolve} = require('path')
const target = args._[0] || 'reactivity'
const format = args.f || 'global'


const pkg = require(resolve(__dirname, `../packages/${target}/package.json`))
const {build} = require("esbuild");
// iife 立即执行函数 (function{})()
// cjs node中的模块 module.export
// ems 浏览器中的esModule模块 import
const outputFormat = format.startsWith('global') ? 'iife' : format === 'cjs' ? 'cjs' : 'esm'
const outfile = resolve(__dirname, `../packages/${target}/dist/${target}.${format}.js`)

build({
  entryPoints: [resolve(__dirname, `../packages/${target}/src/index.ts`)],
  outfile,
  bundle: true, //把所有的包全部打包到一起
  sourcemap: true,
  format: outputFormat,
  globalName: pkg.buildOptions?.name,
  platform: format === 'cjs' ? 'node' : 'browser',
  watch: {
    onRebuild(error) {
      if (!error) console.log('rebuild~~~~')
    }
  }
}).then(() => {
  console.log('watching~~~~')
})
